<?php

require_once 'ActionFramework/start.php';
require_once 'actions/reject_by_url.cls.php';

class SendMessageAction extends Action {
	
	const INPUT_DESTINATION = 'destination';
	const INPUT_MESSAGE = 'message';
	
	const ADMIN = '-1';
	
	public function __construct() {
		parent::__construct();
		$this->add_input(new Input(SendMessageAction::INPUT_DESTINATION));
		$this->add_input(new TextInput(SendMessageAction::INPUT_MESSAGE, 'Message', 1, 500));
	}
	
	public function trigger($request) {
		
		$destination = $request[SendMessageAction::INPUT_DESTINATION];
		$message = strip_tags($request[SendMessageAction::INPUT_MESSAGE]);
		
		if ($destination == SendMessageAction::ADMIN) {
			$mail_address = ADMIN_EMAIL;
			$mail_message = "
Hi,

Someone gave some feedback on the Tandem Delft website.

--

$message
			";
			LT_mail($mail_address, "Feedback on website", $mail_message);
			return new TextView('Message sent!');
		}
		
		
		$user = $_SESSION['user'];
		if (!$user) {
			// Not logged in
			return new TextView('Log in first');
		}
		if ($user->invisible) {
			return new TextView('You are invisible; therefore you can\'t send messages.');
		}
		
		global $DB;
		
		// Is the desitination valid?
		$destination = $DB->get_user_by_id(intval($destination));
		if (!$destination || in_array($user->user_id, $destination->rejections) || $destination->invisible) {
			// TODO: user must be in match list too? (To avoid random guesses of user IDs)
			return new TextView('Unknown destination');
		}
		
		// Can we still send messages today?
		$yesterday = new Date();
		$yesterday->addDays(-1);
		if ($DB->messages_sent_by_user_since($user, $yesterday) >= 5) {
			return new TextView("Sorry, you can only sent 5 messages a day.");
		}
		
		$DB->log_message_sent($user, $destination);
		
		$login_link = r(new LoginByUrlAction())->get_direct_link();
		$login_link->set_value(LoginByUrlAction::INPUT_SECRET, $DB->create_login_by_url_secret($destination));
		$login_link = $login_link->as_url();
		
		$block_link = r(new RejectByUrlAction())->get_direct_link();
		$block_link->set_value(RejectByUrlAction::INPUT_REJECTED_USER_ID, $user->user_id);
		$block_link->set_value(RejectByUrlAction::INPUT_SECRET, $DB->create_login_by_url_secret($destination));
		$block_link = $block_link->as_url();
		
		$mail_address = $destination->email;
		$mail_message = "Hi {$destination->first_name},

You received a message from {$user->first_name} {$user->last_name} on the Tandem Delft website.

--

$message

--

Follow this link to view all your match results, including {$user->first_name}: {$login_link}

Are you currently not looking for a tandem partner? You can make yourself invisible by going to \"Profile & preferences\".

Is this message spam to you? You can block {$user->first_name} by following this link: {$block_link} 

We do everything we can to stop spam. If you do get spam messages regularly, please contact us so that we can do something about it.

The Language Exchange team

";
		LT_mail($mail_address, "New message from {$user->first_name}", $mail_message);

		// TODO: Enable sleep only to test slow connection.
		// sleep(1);
		
		return new TextView('Message sent!');	
	}
	
}

?>